﻿using System;
using System.Diagnostics;
using System.IO.Ports;
using System.Windows.Forms;
using thinger.DataConvertLib;

namespace SerialPortDemo_01
{
    public partial class MainForm : Form
    {
        private SerialPort serialPort = new SerialPort();

        private bool open = false;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // 获取当前可识别的串口
            string[] port = System.IO.Ports.SerialPort.GetPortNames();

            // 初始化界面
            comboBoxPortName.Items.AddRange(port);
            // 如果有数据显示第0个
            comboBoxPortName.SelectedIndex = comboBoxPortName.Items.Count > 0 ? 0 : -1;

            comboBoxBaudRate.Items.Add(1200);
            comboBoxBaudRate.Items.Add(4800);
            comboBoxBaudRate.Items.Add(9600);
            comboBoxBaudRate.Items.Add(14400);
            comboBoxBaudRate.Items.Add(19200);
            comboBoxBaudRate.Items.Add(38400);

            comboBoxBaudRate.SelectedItem = 9600;
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            try
            {
                if(!open)
                {
                    serialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPortDataReceived);
                    serialPort.PortName = comboBoxPortName.Text;
                    serialPort.BaudRate = int.Parse(comboBoxBaudRate.Text);
                    serialPort.DataBits = 8;
                    serialPort.StopBits = StopBits.One;
                    serialPort.Parity = Parity.None;
                    serialPort.Open();

                    buttonOpen.Text = "关闭串口";
                    open = true;
                }
                else
                {
                    serialPort.Close();

                    buttonOpen.Text = "打开串口";
                    open = false;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                serialPort = (SerialPort)sender;
                byte[] bytebuffer = new byte[serialPort.BytesToRead];
                serialPort.Read(bytebuffer, 0, bytebuffer.Length);

                Invoke(new Action(() => textBoxLog.AppendText("收到响应：" + StringLib.GetHexStringFromByteArray(bytebuffer) + "\r\n")));

            }
            catch (Exception)
            {
                throw;
            }
        }

        private void buttonRead_Click(object sender, EventArgs e)
        {
            try {
                // 0x01 -- 从机地址；  0x03 -- 功能码； 0x00 -- 高位起始地址
                // 0x00 -- 低位起始地址； 0x00 -- 高位读取寄存器个数
                // 0x09 -- 低位读取寄存器个数
                // 0x85 0xCC -- 校验位
                byte[] request = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x64, 0x44, 0x21 };

                //byte[] request = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x9, 0x85, 0xCC };

                textBoxLog.AppendText("发送请求：" + StringLib.GetHexStringFromByteArray(request) + "\r\n");
                serialPort.Write(request, 0, request.Length);
            } catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
